﻿Imports DevComponents.DotNetBar
Imports System.Net
Imports System.Text.RegularExpressions

Public Class FrmCapturaAutobus

    Private Sub AtrasBtn_Click(sender As Object, e As EventArgs) Handles AtrasBtn.Click
        If PageSlider1.SelectedPageIndex = 0 Then
            FrmCatalogoAutobuses.Show()
            _EnTarea = False
            _Editar = False
            Me.Close()
        Else
            PageSlider1.SelectedPageIndex = PageSlider1.SelectedPageIndex - 1
        End If
    End Sub

    Private Sub txt_Enter(sender As Object, e As EventArgs) Handles txtIP.Enter, txtKilometros.Enter, txtMarca.Enter, txtModelo.Enter, txtMotor.Enter, txtNoEconomico.Enter, txtNoInterno.Enter, _
        txtOdometro.Enter, txtPlacas.Enter, txtRendimiento.Enter, txtSerie.Enter, txtTag.Enter, txtTagTanque.Enter
        Dim txt As DevComponents.DotNetBar.Controls.TextBoxX = DirectCast(sender, DevComponents.DotNetBar.Controls.TextBoxX)
        txt.Border.BorderColor2 = SystemColors.MenuHighlight
    End Sub

    Private Sub FrmCapturaAutobus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            'TODO: esta línea de código carga datos en la tabla 'DataSet_Prod_Serv.Empresas' Puede moverla o quitarla según sea necesario.
            Me.EmpresasTableAdapter.Fill(Me.DataSet_Prod_Serv.Empresas)
            'TODO: esta línea de código carga datos en la tabla 'DataSet_Prod_Serv.Rutas' Puede moverla o quitarla según sea necesario.
            Me.RutasTableAdapter.Fill(Me.DataSet_Prod_Serv.Rutas)

            'Todo esto son los colores de los controles que por alguna razón no los toma en sus propiedades del diseñador ಥ_ಥ 
            Me.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            lbVehiculo.ForeColor = Color.DarkGray  'Los controles no cambian su foreColor por alguna extraña razón ಥ_ಥ
            lblNombre.ForeColor = SystemColors.MenuHighlight
            lbSub1.ForeColor = SystemColors.MenuHighlight
            lbSub2.ForeColor = SystemColors.MenuHighlight
            lbSub3.ForeColor = SystemColors.MenuHighlight
            PageSlider1.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            PageSliderPage1.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtNoInterno.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtNoEconomico.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            cbRuta.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtIP.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtKilometros.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtMarca.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtModelo.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtMotor.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtOdometro.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtPlacas.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtRendimiento.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtSerie.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtTag.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtTagTanque.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            txtSerie.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            cbEmpresa.BackColor = StyleManager1.MetroColorParameters.CanvasColor
            slideOpciones.BackColor = Color.Black
            panelOpciones.BackColor = Color.Black

            slideOpciones.IsOpen = False

            If _Editar Then
                Me.AutobusesTableAdapter.Fill(Me.DataSet_Prod_Serv.Autobuses)
                Dim auto() As DataRow = Me.DataSet_Prod_Serv.Autobuses.Select("IDAUTOBUS=" & _IDAUTOBUS)
                cbRuta.SelectedValue = auto.ElementAt(0).Item(1)
                txtNoInterno.Text = auto.ElementAt(0).Item(2).ToString.Trim
                txtNoEconomico.Text = auto.ElementAt(0).Item(3).ToString.Trim
                txtPlacas.Text = auto.ElementAt(0).Item(4).ToString.Trim
                txtMarca.Text = auto.ElementAt(0).Item(5).ToString.Trim
                txtModelo.Text = auto.ElementAt(0).Item(6).ToString.Trim
                txtSerie.Text = auto.ElementAt(0).Item(7).ToString.Trim
                txtMotor.Text = auto.ElementAt(0).Item(8).ToString.Trim
                txtRendimiento.Text = auto.ElementAt(0).Item(10).ToString.Trim
                txtKilometros.Text = auto.ElementAt(0).Item(11).ToString.Trim
                txtTag.Text = auto.ElementAt(0).Item(12).ToString.Trim
                dtDiesel.Value = auto.ElementAt(0).Item(13)
                txtTagTanque.Text = auto.ElementAt(0).Item(14).ToString.Trim
                txtIP.Text = auto.ElementAt(0).Item(16).ToString.Trim
                cbEmpresa.SelectedValue = auto.ElementAt(0).Item(17)
                cbSegmento1.Text = auto.ElementAt(0).Item(18).ToString.Trim
                cbSegmento2.Text = auto.ElementAt(0).Item(19).ToString.Trim
                cbSegmento3.Text = auto.ElementAt(0).Item(20).ToString.Trim
                txtOdometro.Text = auto.ElementAt(0).Item(21).ToString.Trim
                dtFechaAdq.Value = auto.ElementAt(0).Item(22)
            End If
        Catch ex As Exception
            ToastNotification.Show(Me, ex.Message, My.Resources.appbar_cone, 5000, eToastGlowColor.Red, eToastPosition.MiddleCenter)
        End Try
    End Sub

    Private Sub btnSiguiente_Click(sender As Object, e As EventArgs) Handles btnSiguiente.Click
        PageSlider1.SelectedPage = PageSliderPage2
    End Sub

    Private Sub btnMostrarOpciones_Click(sender As Object, e As EventArgs) Handles btnMostrarOpciones.Click
        slideOpciones.IsOpen = True
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        slideOpciones.IsOpen = False
    End Sub

    Private Sub txt_TextChanged(sender As Object, e As EventArgs) Handles txtNoInterno.TextChanged, txtNoEconomico.TextChanged, txtIP.TextChanged, _
        txtKilometros.TextChanged, txtMarca.TextChanged, txtModelo.TextChanged, txtMotor.TextChanged, txtMotor.TextChanged, txtOdometro.TextChanged, txtPlacas.TextChanged, _
        txtRendimiento.TextChanged, txtSerie.TextChanged, txtSerie.TextChanged, txtTag.TextChanged, txtTagTanque.TextChanged
        Dim txt As DevComponents.DotNetBar.Controls.TextBoxX = DirectCast(sender, DevComponents.DotNetBar.Controls.TextBoxX)
        If txt Is txtNoInterno Then
            If txt.Text <> "" Then
                lbNoInterno.Visible = True
            Else
                lbNoInterno.Visible = False
            End If
        End If
        If txt Is txtNoEconomico Then
            If txt.Text <> "" Then
                lbNoEconomico.Visible = True
                lbVehiculo.Text = txt.Text
            Else
                lbNoEconomico.Visible = False
            End If
        End If
        If txt Is txtIP Then
            If txt.Text <> "" Then
                lbIP.Visible = True
            Else
                lbIP.Visible = False
            End If
        End If
        If txt Is txtKilometros Then
            If txt.Text <> "" Then
                lbKilometros.Visible = True
            Else
                lbKilometros.Visible = False
            End If
        End If
        If txt Is txtMarca Then
            If txt.Text <> "" Then
                lbMarca.Visible = True
            Else
                lbMarca.Visible = False
            End If
        End If
        If txt Is txtModelo Then
            If txt.Text <> "" Then
                lbMarca.Visible = True
            Else
                lbModelo.Visible = False
            End If
        End If
        If txt Is txtMotor Then
            If txt.Text <> "" Then
                lbMotor.Visible = True
            Else
                lbMotor.Visible = False
            End If
        End If
        If txt Is txtOdometro Then
            If txt.Text <> "" Then
                lbOdometro.Visible = True
            Else
                lbOdometro.Visible = False
            End If
        End If
        If txt Is txtPlacas Then
            If txt.Text <> "" Then
                lbPlacas.Visible = True
            Else
                lbPlacas.Visible = False
            End If
        End If
        If txt Is txtRendimiento Then
            If txt.Text <> "" Then
                lbRendimiento.Visible = True
            Else
                lbRendimiento.Visible = False
            End If
        End If
        If txt Is txtSerie Then
            If txt.Text <> "" Then
                lbSerie.Visible = True
            Else
                lbSerie.Visible = False
            End If
        End If
        If txt Is txtTag Then
            If txt.Text <> "" Then
                lbTag.Visible = True
            Else
                lbTag.Visible = False
            End If
        End If
        If txt Is txtTagTanque Then
            If txt.Text <> "" Then
                lbTagTanque.Visible = True
            Else
                lbTagTanque.Visible = False
            End If
        End If

    End Sub

    Private Sub SuperValidator1_CustomValidatorValidateValue(sender As Object, e As Validator.ValidateValueEventArgs) Handles SuperValidator1.CustomValidatorValidateValue
        Dim re As Regex
        re = New Regex("[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]\.[0-2][0-9][0-9]")
        If re.Match(e.ControlToValidate.Text).Success Then
            e.IsValid = True
        Else
            e.IsValid = False
        End If
    End Sub

    Private Sub txt_ValidaNumero(sender As Object, e As KeyPressEventArgs) Handles txtRendimiento.KeyPress, txtKilometros.KeyPress, txtOdometro.KeyPress
        Dim txt As DevComponents.DotNetBar.Controls.TextBoxX = DirectCast(sender, DevComponents.DotNetBar.Controls.TextBoxX)
        NumeroDec(e, txt)
    End Sub

    Private Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub btnGuardar_Click(sender As Object, e As EventArgs) Handles btnGuardar.Click
        Try
            If txtRendimiento.Text = "" Then
                txtRendimiento.Text = 0
            End If
            If txtKilometros.Text = "" Then
                txtKilometros.Text = 0
            End If
            If txtOdometro.Text = "" Then
                txtOdometro.Text = 0
            End If
            If txtNoInterno.Text = "" Or txtNoEconomico.Text = "" Then
                ToastNotification.Show(Me, "Debe de capturar cuando menos el No.Interno y No.Economico de la unidad para poder guardar el autobus.", My.Resources.appbar_cone, 5000, eToastGlowColor.Red, eToastPosition.MiddleCenter)
                Exit Sub
            End If
            If _Editar = False Then
                autobus.Insert(cbRuta.SelectedValue, txtNoInterno.Text, txtNoEconomico.Text, txtPlacas.Text, txtMarca.Text, txtModelo.Text, txtSerie.Text, txtMotor.Text, "Alta", Decimal.Parse(txtRendimiento.Text), _
                               Decimal.Parse(txtKilometros.Text), txtTag.Text, dtDiesel.Value, txtTagTanque.Text, 0, txtIP.Text, cbEmpresa.SelectedValue, cbSegmento1.Text, cbSegmento2.Text, cbSegmento3.Text, _
                               Decimal.Parse(txtOdometro.Text), dtFechaAdq.Value)
                ToastNotification.Show(Me, "Capturado con exito", My.Resources.add1, 4000, eToastGlowColor.Blue, eToastPosition.TopCenter)
                Limpia_Todo()
            Else
                autobus.Update(cbRuta.SelectedValue, txtNoInterno.Text, txtNoEconomico.Text, txtPlacas.Text, txtMarca.Text, txtModelo.Text, txtSerie.Text, txtMotor.Text, "Alta", Decimal.Parse(txtRendimiento.Text), _
                               Decimal.Parse(txtKilometros.Text), txtTag.Text, dtDiesel.Value, txtTagTanque.Text, 0, txtIP.Text, cbEmpresa.SelectedValue, cbSegmento1.Text, cbSegmento2.Text, cbSegmento3.Text, Decimal.Parse(txtOdometro.Text), dtFechaAdq.Value, _
                               _IDAUTOBUS)
                ToastNotification.Show(Me, "Actualizado con exito", My.Resources.add2, 4000, eToastGlowColor.Blue, eToastPosition.TopCenter)
            End If
        Catch ex As Exception
            ToastNotification.Show(Me, ex.Message, My.Resources.appbar_cone, 5000, eToastGlowColor.Red, eToastPosition.MiddleCenter)
        End Try
    End Sub

    Public Sub Limpia_Todo()
        Try
            For Each ctrl As Control In PageSliderPage1.Controls
                If TypeOf ctrl Is DevComponents.DotNetBar.Controls.TextBoxX Then
                    CType(ctrl, DevComponents.DotNetBar.Controls.TextBoxX).Text = String.Empty
                ElseIf TypeOf ctrl Is DevComponents.DotNetBar.Controls.ComboBoxEx Then
                    CType(ctrl, DevComponents.DotNetBar.Controls.ComboBoxEx).SelectedIndex = 0
                End If
            Next
            For Each ctrl As Control In PageSliderPage2.Controls
                If TypeOf ctrl Is DevComponents.DotNetBar.Controls.TextBoxX Then
                    CType(ctrl, DevComponents.DotNetBar.Controls.TextBoxX).Text = String.Empty
                ElseIf TypeOf ctrl Is DevComponents.DotNetBar.Controls.ComboBoxEx Then
                    CType(ctrl, DevComponents.DotNetBar.Controls.ComboBoxEx).SelectedIndex = 0
                End If
            Next
        Catch
        End Try
    End Sub
End Class